/*
 * Copyright (c) 2018 FA Software (Shanghai) Co., Ltd. All Rights Reserved. Proprietary And Confidential.
 */
package com.rtline.business.utils;

import java.util.List;
import java.util.Objects;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

/**
 * excel全局缓存
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ValidateGlobalCacheUtils {

    private static final String EXCEL_PRE_KEY = "excel_";

    private static final String EXCEL_VALIDATE_FAIL_PRE_KEY = "excel_validate_fail_";

    private static final String EXCEL_VALIDATE_SUCCESS_PRE_KEY = "excel_validate_success_";

    private static final TimedCache<String, Object> CACHE = CacheUtil.newTimedCache(30 * 60 * 1000L); // 设置过期时间为30分钟

    /**
     * 导入数据放入缓存
     *
     * @param importIdent 导入标识
     * @param excelData   导入数据
     */
    public static <D> void putExcelData(String importIdent, List<D> excelData) {
        CACHE.put(EXCEL_PRE_KEY + importIdent, excelData);
    }

    /**
     * 从缓存中取出导入数据
     *
     * @param importIdent 导入标识
     * @return 导入数据
     */
    @SuppressWarnings("unchecked")
    public static <D> List<D> getExcelData(String importIdent) {
        Object obj = getData(EXCEL_PRE_KEY, importIdent);
        return ObjectUtil.isNull(obj) ? CollUtil.newLinkedList() : (List<D>) obj;
    }

    /**
     * 校验失败的数据放入缓存
     *
     * @param importIdent 导入标识
     * @param failData    校验失败的数据
     */
    public static <D> void putValidateFailData(String importIdent, List<D> failData) {
        CACHE.put(EXCEL_VALIDATE_FAIL_PRE_KEY + importIdent, failData);
    }

    /**
     * 从缓存中取出校验失败的数据
     *
     * @param importIdent 导入标识
     * @return 校验失败的数据
     */
    @SuppressWarnings("unchecked")
    public static <D> List<D> getValidateFailData(String importIdent) {
        Object obj = getData(EXCEL_VALIDATE_FAIL_PRE_KEY, importIdent);
        return ObjectUtil.isNull(obj) ? CollUtil.newLinkedList() : (List<D>) obj;
    }

    /**
     * 校验成功的数据放入缓存
     *
     * @param importIdent 导入标识
     * @param successData 校验成功的数据
     */
    public static <D> void putValidateSuccessData(String importIdent, List<D> successData) {
        CACHE.put(EXCEL_VALIDATE_SUCCESS_PRE_KEY + importIdent, successData);
    }

    /**
     * 从缓存中取出校验成功的数据
     *
     * @param importIdent 导入标识
     * @return 校验成功的数据
     */
    @SuppressWarnings("unchecked")
    public static <D> List<D> getValidateSuccessData(String importIdent) {
        Object obj = getData(EXCEL_VALIDATE_SUCCESS_PRE_KEY, importIdent);
        return ObjectUtil.isNull(obj) ? CollUtil.newLinkedList() : (List<D>) obj;
    }

    private static Object getData(String key, String importIdent) {
        Object data = CACHE.get(key + importIdent);
        if (Objects.nonNull(data)) {
            return data;
        } else {
            // 重新读取文件
            return null;
        }
    }
}
